package tree;

public class TestTree {



public  static void  main(String []args){
    TreeNode root=new TreeNode("1");
    TreeNode treeNode21=new TreeNode("2");
    TreeNode treeNode22=new TreeNode("3");
    root.left=treeNode21;
    root.right=treeNode22;
    TreeNode treeNode31=new TreeNode("4");
    treeNode21.left=treeNode31;
    TreeNode treeNode32=new TreeNode("5");
    treeNode22.right=treeNode32;
    TreeNode treeNode41=new TreeNode("6");
    treeNode31.right=treeNode41;
    TreeNode treeNode51=new TreeNode("7");
    treeNode41.left=treeNode51;
    TreeNode treeNode52=new TreeNode("8");
    treeNode51.right=treeNode52;
    recursionPreorderTraversal(root);
    System.out.println("___________________________");
    recursionMiddleorderTraversal(root);
    System.out.println("___________________________");
    recursionPostorderTraversal(root);
}

    // 递归先序遍历
    public static void recursionPreorderTraversal(TreeNode root) {
        if (root != null) {
            System.out.print(root.val + " ");
            recursionPreorderTraversal(root.left);
            recursionPreorderTraversal(root.right);
        }
    }

    // 递归中序遍历
    public static void recursionMiddleorderTraversal(TreeNode root) {
        if (root != null) {
            recursionMiddleorderTraversal(root.left);
            System.out.print(root.val + " ");
            recursionMiddleorderTraversal(root.right);
        }
    }

    // 递归后序遍历
    public static void recursionPostorderTraversal(TreeNode root) {
        if (root != null) {
            recursionPostorderTraversal(root.left);
            recursionPostorderTraversal(root.right);
            System.out.print(root.val + " ");
        }
    }

}
class TreeNode {
    String val;
    //左子树
    TreeNode left;
    //右子树
    TreeNode right;
    //构造方法
    TreeNode(String x) {
        val = x;
    }
}
